<template>
  <div class="contain">
    <el-card style="margin: 20px">
      <!-- 第一行标签 -->
      <el-form label-width="100px">
        <el-row :gutter="3">
          <el-col :span="6">
            <el-form-item label="标签名称" style="margin-bottom: 2px">
              <el-input
                v-model="searFrom.tags"
                size="small"
                placeholder="请输入企业标签"
              />
            </el-form-item>
          </el-col>
          <!-- 选择城市和区 -->
          <el-col :span="6">
            <el-form-item label="城市" style="margin-bottom: 2px">
              <el-select v-model="province" size="small">
                <el-option
                  v-for="item in Area"
                  :key="item.value"
                  :label="item.label"
                  :value="item"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="地区" style="margin-bottom: 2px">
              <el-select v-model="city" size="small">
                <el-option
                  v-for="item in cityQuery"
                  :key="item.value"
                  :label="item.label"
                  :value="item"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="企业简称" style="margin-bottom: 2px">
              <el-input
                v-model="searFrom.shortName"
                size="small"
                placeholder="请输入企业简称"
              />
            </el-form-item>
          </el-col>
        </el-row>

        <!-- 第二行标签 -->
        <el-row style="margin-top: 20px">
          <el-form-item label="状态">
            <el-col :span="18">
              <el-select
                v-model.trim="searFrom.state"
                style="width: 200px; margin-right: 15px"
                placeholder="请选择企业状态"
                size="small"
              >
                <el-option :value="1" label="禁用">禁用</el-option>
                <el-option :value="0" label="启用">启用</el-option>
              </el-select>

              <el-button size="small" @click="handelMationClean">
                清空
              </el-button>
              <el-button type="primary" size="small" @click="getCompanyList">
                搜索
              </el-button>
            </el-col>
            <el-col :span="6" style="text-align: right">
              <el-button
                type="success"
                class="right"
                size="small"
                @click="addDialogShow"
              >
                新增企业
              </el-button>
            </el-col>
          </el-form-item>
        </el-row>
      </el-form>
      <!-- 提示文案 -->
      <el-alert type="info" :closable="false">
        <i class="el-icon-info" />共{{ total }}条记录</el-alert
      >
      <!-- 表单区域 -->
      <el-table :data="companyList">
        <el-table-column label="序号" prop="id" />
        <el-table-column label="企业编号" prop="number" />
        <el-table-column label="企业简称" prop="company" />
        <el-table-column label="标签" prop="tags" />
        <el-table-column label="创建者" prop="creatorID" />
        <el-table-column label="创建日期" prop="addDate" />
        <el-table-column label="备注" prop="remarks" />

        <el-table-column label="状态" prop="state">
          <template #default="{ row }">
            <el-switch
              :value="row.state"
              active-color="#13ce66"
              inactive-color="#ccc"
              :active-value="0"
              :inactive-value="1"
              :disabled="true"
            />
          </template>
        </el-table-column>
        <el-table-column label="操作" width="190">
          <template #default="{ row }">
            <div>
              <!-- 修改按钮 -->
              <el-tooltip content="修改" placement="top" effect="dark">
                <el-button
                  type="primary"
                  icon="el-icon-edit"
                  circle
                  plain
                  @click="handelRestUser(row)"
                />
              </el-tooltip>
              <!-- 禁用按钮 -->
              <el-tooltip
                v-if="row.state === 1"
                content="禁用"
                placement="top"
                effect="dark"
              >
                <el-button
                  type="warning"
                  icon="el-icon-close"
                  circle
                  plain
                  @click="resetCompanyState(row)"
                />
              </el-tooltip>
              <el-tooltip v-else content="启用" placement="top" effect="dark">
                <el-button
                  type="success"
                  icon="el-icon-check"
                  circle
                  plain
                  @click="resetCompanyState(row)"
                />
              </el-tooltip>
              <!-- 删除按钮 -->
              <el-tooltip content="删除" placement="top" effect="dark">
                <el-button
                  type="danger"
                  plain
                  icon="el-icon-delete"
                  circle
                  @click="delCompany(row)"
                />
              </el-tooltip>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <div class="block" style="text-align: right; margin-top: 10px">
        <el-pagination
          :current-page="searFrom.page"
          :page-sizes="[5, 8, 10, 15]"
          :page-size="searFrom.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
    <!-- 新增企业提示框 -->
    <add-company :show-dialog.sync="showDialog" @add-success="getCompanyList" />
    <!-- 编辑企业弹框 -->
    <edit-company
      :show-dialog.sync="showeditDialog"
      :edit-form="editForm"
      @add-success="getCompanyList"
    />
  </div>
</template>

<script>
import {
  getCompanyListAPI,
  delCompanyAPI,
  resetCompanyStateAPI
} from '@/api/company'
// 导入组件
import { citys } from '@/components/MyCity'
import editCompany from './components/editCompany.vue'
import AddCompany from './components/addCompany.vue'
export default {
  name: 'Company',
  components: { editCompany, AddCompany },
  data() {
    return {
      Area: [],
      cityQuery: [],
      areaQuery: [],
      province: '',
      city: '',
      area: '',
      // 新增用户组件是否显示
      showDialog: false,
      showeditDialog: false,
      // 搜索表单对象
      searFrom: {
        page: 1,
        pagesize: 10
        // tags: '',
        // city: '',
        // province: '',
        // state: '',
        // shortName: ''
      },
      companyList: [],
      total: 0,
      editForm: {}
    }
  },
  watch: {
    // 监听省级
    province: function (newValue) {
      this.cityQuery = []
      this.shi = ''
      console.log(this.Area)
      this.Area.forEach((item) => {
        if (newValue.label === item.label) {
          this.cityQuery = item.children
          console.log('city', this.cityQuery)
        }
      })
    }
  },
  created() {
    this.getCompanyList()
    this.Area = citys
  },
  methods: {
    // 企业禁用
    async resetCompanyState(comData) {
      try {
        if (comData.state === 1) {
          await this.$confirm('确定禁用该企业吗？', '温馨提示', {
            type: 'warning'
          })
          await resetCompanyStateAPI(comData.id, 0)
          this.$message.success('禁用成功🤡')
        } else {
          await this.$confirm('确定启用该企业吗？', '温馨提示', {
            type: 'warning'
          })
          await resetCompanyStateAPI(comData.id, 1)
          this.$message.success('启用成功🤡')
        }
      } catch (error) {
        console.log(error)
      } finally {
        this.getCompanyList()
      }
    },
    // 当条数改变时
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.searFrom.pagesize = val
      this.getCompanyList()
    },
    // 当页数改变时
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.searFrom.page = val
      this.getCompanyList()
    },
    // 打开新增企业提示框
    addDialogShow() {
      this.showDialog = true
    },
    // 删除企业
    async delCompany(company) {
      try {
        await this.$confirm('确定删除该企业吗？', '温馨提示', {
          type: 'warning'
        })
        await delCompanyAPI(company)
        this.$message.success('删除企业成功🐕')
        // console.log(resp)
        this.getCompanyList()
      } catch (error) {
        console.log(error)
      }
    },
    // 清空表单
    handelMationClean() {
      this.searFrom = {}
      this.city = this.province = ''
      this.getCompanyList()
    },
    // 获取企业列表
    async getCompanyList() {
      if (this.city !== '' || this.province !== '') {
        this.searFrom.city = this.city.label
        this.searFrom.province = this.province.label
      }
      const { data } = await getCompanyListAPI(this.searFrom)
      // console.log(data)
      this.companyList = data.items
      this.total = data.counts
    },
    // 打开编辑企业提示框
    handelRestUser(e) {
      this.showeditDialog = true
      // console.log(e)
      this.editForm = e
    }
  }
}
</script>

<style lang="scss" scoped>
</style>

